//
//  MXArcView.m
//  MXArcChart
//
//  Created by 马彦 on 16/11/4.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXCircleArcView.h"

@interface MXCircleArcView ()

/**
 *  总数值
 */
@property (nonatomic, assign)CGFloat totalVale;

@end

@implementation MXCircleArcView


- (void)setValueDataArray:(NSArray *)valueDataArray {
    _valueDataArray = valueDataArray;
    
    [self arcValueConfig];
}

- (void)arcValueConfig {
    for (id obj in _valueDataArray) {
        self.totalVale += [obj floatValue];
    }
}

- (void)setColorArray:(NSArray<UIColor *> *)colorArray {
    _colorArray = colorArray;
}

- (void)drawArcWithtext:(NSString *)text center:(CGPoint)center Radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color {
    
        /**画圆弧*/
        [self chartBaseARCWithcenter:center radius:radius startAngle:startAngle endAngle:endAngle color:color];
        /**圆弧分割线*/
        [self chartBaseWithcenter:center radius:radius startAngle:startAngle endAngle:endAngle color:color];
        /**指示线*/
        [self drawLineWithtext:text center:center radius:radius startAngle:startAngle endAngle:endAngle color:color];
}


- (void)drawRect:(CGRect)rect {
    [super drawRect:self.frame];
    
    if (!_valueDataArray.count) {
        return;
    }
    
    CGFloat radius = 0;
    if (self.width >= self.height) {
        radius = self.height * 0.2;
    } else {
        radius = self.width * 0.2;
    }
    CGFloat bigAngle = -M_PI_2;
    for (NSInteger i = 0; i < _valueDataArray.count; i++) {
        CGFloat endAngle = (M_PI * 2.0) * [_valueDataArray[i] floatValue] / self.totalVale + bigAngle;
        NSString *str = [NSString stringWithFormat:@"%.1lf%c",[_valueDataArray[i] floatValue] / self.totalVale * 100,'%'];
        
        [self drawArcWithtext:str center:CGPointMake(self.width * 0.5, self.height * 0.5) Radius:radius startAngle:bigAngle endAngle: endAngle color:_colorArray[i]];
        
        bigAngle = endAngle;
    }
}

- (void)setSliceSpace:(CGFloat)sliceSpace {
    if (sliceSpace <= 0) {
        sliceSpace = 0;
    } else if (sliceSpace >= 20) {
        sliceSpace = 10;
    } else {
        sliceSpace = sliceSpace * 0.5;
    }
    
    [super setSliceSpace:sliceSpace];
}



@end
